package com.detrum.sportcamera.nfsnet;

import android.content.Context;

import com.detrum.NFSVideo;
import com.detrum.base.BaseStatic;
import com.detrum.bean.FileBean;
import com.detrum.bean.VersionBean;
import com.detrum.sportcamera.nfstask.GetFilesCall;
import com.detrum.sportcamera.nfstask.LoadSingleCall;
import com.detrum.sportcamera.nfstask.NfsDeleteListTask;
import com.detrum.sportcamera.nfstask.NfsDownLoadTask;
import com.detrum.sportcamera.nfstask.NfsGetFiles;
import com.detrum.sportcamera.nfstask.NfsLoadSingle;
import com.detrum.sportcamera.nfstask.NfsWriteDeviceFile;
import com.detrum.sportcamera.nfstask.WriteDevcieFileCall;
import com.detrum.sqlite.SqliteVersionOperate;
import com.detrum.utils.DeviceFinal;
import com.detrum.utils.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

/**
 * Created by chenlei on 2017/10/19.
 */

public class NfsRequestManager {

    private static NfsRequestManager nfsRequestManager;

    private Context context;

    private Vector<Integer> vector = new Vector();

    private Vector<Integer> vector2 = new Vector<>();

    private static final String TAG = "NfsRequestManager";

    private HashMap<Integer, Boolean> hashMap = new HashMap<>();//判断指定index是否mount

    private NfsRequestManager(Context context){
        this.context = context;
        for (int i = 0; i < 5; i++) {
            vector.add(i);
            hashMap.put(i, false);
        }
        for (int i = 5; i < 10; i++) {
            vector2.add(i);
            hashMap.put(i,false);
        }
    }

    public static NfsRequestManager getInstence(Context context){
        if(nfsRequestManager==null){
            synchronized (NfsRequestManager.class) {
                if (nfsRequestManager == null) {
                    nfsRequestManager = new NfsRequestManager(context);
                }
            }
        }
        return nfsRequestManager;
    }

    /**
     * 更新设备文件写入线程
     * @param nfsManagerCall6
     * @return
     */
    public int UpdateDeviceFile(final NfsManagerCall6 nfsManagerCall6){
        final int index = getIndex(false);
        if (index == -1) {
            nfsManagerCall6.indexFail();
            return -1;
        }
        NfsWriteDeviceFile nfsWriteDeviceFile = new NfsWriteDeviceFile(index, new WriteDevcieFileCall() {
            @Override
            public void FileEmpty() {
                closeIndex(index);
                nfsManagerCall6.fileEmpty();
            }

            @Override
            public void DeviceUpdate(int position) {
                nfsManagerCall6.updateDevice(position);
                LogUtils.e("NfsRequestManager position = "+position);
            }

            @Override
            public void finish() {
                LogUtils.e("NfsRequestManager finish");
                closeIndex(index);
                nfsManagerCall6.finish();
            }
        });
        VersionBean bean = SqliteVersionOperate.getInstance(context).Select("DCAM-H");
        if(bean==null){
            return -2;
        }
        BaseStatic.deviceLocalPath = bean.getLocalFilePath();
        if(BaseStatic.deviceLocalPath==null||BaseStatic.deviceLocalPath.length()==0){
            return -3;
        }
        nfsWriteDeviceFile.execute(BaseStatic.deviceLocalPath);
        return 1;
    }

    /**
     * 设备媒体文件列表获取线程
     * @param nfsManagerCall
     * @return
     */
    public NfsGetFiles getNfsFileList(final NfsManagerCall nfsManagerCall){
        final int index = getIndex(false);
        LogUtils.e("index = "+index);
        if (index == -1) {
            nfsManagerCall.connectFail();
            return null;
        }
        NfsGetFiles nfsGetFiles = new NfsGetFiles(index, context, new GetFilesCall() {
            @Override
            public void sendList(FileBean filebean) {
                nfsManagerCall.getFiles(filebean);
            }

            @Override
            public void finish() {
                nfsManagerCall.finish();
                closeIndex(index);
            }

            @Override
            public void fail() {
                nfsManagerCall.connectFail();
                closeIndex(index);
            }
        });
       // DeviceFinal.NFS_FOLDER;
        nfsGetFiles.execute(DeviceFinal.NFS_FOLDER);
        return nfsGetFiles;
    }

    /**
     * 设备单个媒体文件缩略图获取线程
     * @param beanList
     * @param nfsManagerCall3
     * @return
     */
    public NfsLoadSingle getNfsSingle(ArrayList<FileBean>beanList, final NfsManagerCall3 nfsManagerCall3){
        final int index = getIndex(false);
        //LogUtils.i("getNfsSingle index = "+index+"------------------------");
        if (index == -1) {
            nfsManagerCall3.indexFail();
            return null;
        }
        NfsLoadSingle nfsLoadSingle = new NfsLoadSingle(context, beanList, index,new LoadSingleCall() {
            @Override
            public void loadImageStart(FileBean fileBean) {
                nfsManagerCall3.sendImageStart(fileBean);
            }

            @Override
            public void sendSingle(FileBean fileBean,int position) {
                LogUtils.i("getNfsSingle sendSingle-----------------------");
                nfsManagerCall3.sendSingle(fileBean,position);
            }

            @Override
            public void avGetFail(FileBean fileBean) {
                LogUtils.i("getNfsSingle avGetFail");
                nfsManagerCall3.avGetFail(fileBean);
            }

            @Override
            public void loadFinish() {
                LogUtils.i("getNfsSingle loadFinish");
                nfsManagerCall3.loadfinish();
                closeIndex(index);
            }
        });
        nfsLoadSingle.execute();
        return nfsLoadSingle;
    }

    /**
     * 设备媒体文件下载线程
     * @param nfsManagerCall4
     * @param fileBeans
     * @return
     */
    public NfsDownLoadTask nfsDownloadList(final NfsManagerCall4 nfsManagerCall4, FileBean... fileBeans){
        final int index = getIndex(false);
        if (index == -1) {
            nfsManagerCall4.indexFail();
            return null;
        }
        NfsDownLoadTask nfsDownLoadTask = new NfsDownLoadTask(context, index, new NfsDownLoadTask.NfsDownLoadCall() {
            @Override
            public void finish() {
                nfsManagerCall4.finishAll();
                closeIndex(index);
            }

            @Override
            public void itemStart(String fileName,String num) {
                nfsManagerCall4.itemStart(fileName,num);
            }

            @Override
            public void itemFinish(String fileName,String num, int currentSize) {
                nfsManagerCall4.itemComplete(fileName, num,currentSize);
            }

            @Override
            public void itemFail(String fileName) {
                nfsManagerCall4.itemFail(fileName);
            }

            @Override
            public void countSize(int countSize) {
                nfsManagerCall4.countSize(countSize);
            }

            @Override
            public void itemVideoSize(String fileName,String num, int videoSize) {
                nfsManagerCall4.itemVideoSize(fileName,num,videoSize);
            }
        });
        nfsDownLoadTask.execute(fileBeans);
        return nfsDownLoadTask;
    }

    /**
     * 设备媒体文件删除线程
     * @param fileBeans
     * @param nfsManagerCall5
     * @return
     */
    public NfsDeleteListTask nfsDeleteList(FileBean[]fileBeans, final NfsManagerCall5 nfsManagerCall5){
        final int index = getIndex(false);
        if(index==-1){
            nfsManagerCall5.indexFail();
            return null;
        }
        NfsDeleteListTask nfsDeleteListTask = new NfsDeleteListTask(context, index, new NfsDeleteListTask.NfsDeleteListCall() {
            @Override
            public void itemComplete(String fileName) {
                nfsManagerCall5.itemComplete(fileName);
            }

            @Override
            public void itemFail(String fileName) {
                nfsManagerCall5.itemFail(fileName);
            }

            @Override
            public void finishAll() {
                nfsManagerCall5.finishAll();
                closeIndex(index);
            }
        });
        nfsDeleteListTask.execute(fileBeans);
        return nfsDeleteListTask;
    }

    /**
     * 关闭指定index 主要用于视频播放
     * @param index
     */
    public void closeIndex2(int index){
        hashMap.put(index,false);
        vector2.add(index);
    }

    public void closeIndex(int index){
        vector.add(index);
    }

    public void closeAllIndex(){
        vector2.clear();
        for (int i = 5; i < 10; i++) {
            hashMap.put(i,false);
            vector2.add(i);
        }
    }

    /**
     * 获取nfs登录名,多线程打开设备文件用
     *
     * @return
     */
    private int getIndex(boolean isVideo) {
        if (!isInit) {
            init();
        }
        int index = -1;
        if(isVideo){
           if(vector2.size()>0){
               index = vector2.remove(0);
               if(!hashMap.get(index)){
                       LogUtils.i("getIndex vector2 "+index);
                       hashMap.put(index,true);
                       return index;
               }else{
                   return -1;
               }
           }else{
               return -1;
           }
        }else{
            LogUtils.i("getIndex vector size is "+vector.size());
            if (vector.size() > 0) {
                index = vector.remove(0);
                LogUtils.i("getIndex vector index is "+index);
                if(!hashMap.get(index)){
                    if(nfsMount(index)){
                        LogUtils.i("getIndex vector "+index+" new mount");
                        return index;
                    }else{
                        LogUtils.i("getIndex vector "+index+" new mount fail");
                        vector.add(index);//mount失败release，然后将index重新放回vector
                        return -1;
                    }
                }else{
                    LogUtils.i("getIndex vector "+index+" is mount");
                    return index;
                }
            } else {
                return -1;
            }
        }
    }

    private boolean isInit;

    private boolean nfsMount(int index) {
        // TODO: 2018/1/19 设备测试
        //int flag = NFSVideo.mount(CameraFinal.TEXT_ADDRESS, CameraFinal.TEXT_PATH, index);
        int flag = NFSVideo.mount(BaseStatic.DHCP_SERVICE_IP, DeviceFinal.NFS_PATH, index);
        LogUtils.i("BaseStatic.DHCP_SERVICE_IP = "+BaseStatic.DHCP_SERVICE_IP+":"+DeviceFinal.NFS_PATH);
        if (flag >= 0) {
            hashMap.put(index,true);
            return true;
        } else {
            LogUtils.i("nfsMount flag is "+flag);
            hashMap.put(index,false);
            NFSVideo.release(index);
            return false;
        }
    }

    private void init() {
        NFSVideo.init();
        isInit = true;
    }

    /**
     * 异常情况下释放index
     */
    public void release(int index){
        NFSVideo.release(index);
    }

    /**
     * 正常情况下close,然后释放
     */
    public void close(){
        for (int i = 0; i < vector.size(); i++) {
            int index = vector.get(i);
            NFSVideo.release(index);
        }
        isInit = false;
    }

}
